package com.my.app.tools.util;

import com.my.app.tools.pojo.AppConstants;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;

import java.io.DataOutputStream;
import java.net.HttpURLConnection;
import java.net.URL;

/**
 * http 工具类
 */
public class HttpPost {

    public static String post(String requestUrl, String accessToken, String params) {
        String contentType = "application/x-www-form-urlencoded";
        return HttpPost.post(requestUrl, accessToken, contentType, params);
    }

    public static String post(String requestUrl, String accessToken, String contentType, String params) {
        String encoding = AppConstants.ENCODING_UTF8;
        if (requestUrl.contains("nlp")) {
            encoding = AppConstants.ENCODING_GBK;
        }
        return HttpPost.post(requestUrl, accessToken, contentType, params, encoding);
    }

    public static String post(String requestUrl, String accessToken, String contentType, String xAuthorization, String params) {
        String encoding = AppConstants.ENCODING_UTF8;
        if (requestUrl.contains("nlp")) {
            encoding = AppConstants.ENCODING_GBK;
        }
        return HttpPost.post(requestUrl, accessToken, contentType, xAuthorization, params, encoding);
    }

    public static String post(String requestUrl, String accessToken, String contentType, String xAuthorization, String params, String encoding) {
        String url = requestUrl;
        if (StringUtils.isNotBlank(accessToken)) {
            url = url.concat("?access_token=").concat(accessToken);
        }
        return HttpPost.postGeneralUrl(url, contentType, xAuthorization, params, encoding);
    }

    private static String postGeneralUrl(String generalUrl, String contentType, String xAuthorization, String params, String encoding) {
        try {
            URL url = new URL(generalUrl);
            // 打开和URL之间的连接
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setRequestMethod("POST");
            // 设置通用的请求属性
            if (StringUtils.isNotBlank(contentType)) {
                connection.setRequestProperty("Content-Type", contentType);
            }
            if (StringUtils.isNotBlank(xAuthorization)) {
                connection.setRequestProperty("x-authorization", contentType);
            }
            connection.setRequestProperty("Connection", "Keep-Alive");
            connection.setUseCaches(false);
            connection.setDoOutput(true);
            connection.setDoInput(true);

            // 得到请求的输出流对象
            if (StringUtils.isNotBlank(params)) {
                DataOutputStream out = new DataOutputStream(connection.getOutputStream());
                out.write(params.getBytes(encoding));
                out.flush();
                out.close();
            }

            // 建立实际的连接
            connection.connect();

            String result = IOUtils.toString(connection.getInputStream(), encoding);
            connection.disconnect();
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            LogUtil.error(e.getMessage());
            return null;
        }
    }
}
